package idv.sais.ezcin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageButton;

public class KeyButton extends ImageButton {
	private String[] texts;
	
	public KeyButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void setText(String[] texts)
	{
		this.texts = texts;
		this.invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);

		if (texts == null || texts.length == 0)
			return;
		
		// Center Text
		Paint pBlack = new Paint();
		pBlack.setColor(Color.BLACK);
		pBlack.setTextSize(24);
		pBlack.setAntiAlias(true);
		pBlack.setTextAlign(Paint.Align.valueOf("CENTER"));
		canvas.drawText(texts[0], 1 + (texts[0].length() == 3 ? 3 : 0) + getWidth() / 2, this.getHeight() / 2 + 7, pBlack);
		
		// Border Text URDL
		Paint pGray = new Paint();
		pGray.setColor(Color.GRAY);
		pGray.setTextSize(20);
		pGray.setAntiAlias(true);
		
		switch(texts.length)
		{
		case 5:
			//Left
			pGray.setTextAlign(Paint.Align.valueOf("LEFT"));
			if (texts[4].length() == 2) {
				canvas.drawText(texts[4].substring(0, 1), 5, getHeight() / 2 - 5, pGray);
				canvas.drawText(texts[4].substring(1, 2), 5, getHeight() / 2 + 12, pGray);
			} else {
				canvas.drawText(texts[4], 5, getHeight() / 2 + 6, pGray);
			}
		case 4:
			//Bottom
			pGray.setTextAlign(Paint.Align.valueOf("CENTER"));
			canvas.drawText(texts[3], 1 + getWidth() / 2, getHeight() - 10, pGray);
		case 3:
			//Right
			pGray.setTextAlign(Paint.Align.valueOf("LEFT"));
			if (texts[2].length() == 2) {
				canvas.drawText(texts[2].substring(0, 1), getWidth() - 25, getHeight() / 2 - 5, pGray);
				canvas.drawText(texts[2].substring(1, 2), getWidth() - 25, getHeight() / 2 + 12, pGray);
			} else {
				canvas.drawText(texts[2], getWidth() - 25, getHeight() / 2 + 6, pGray);
			}
		case 2:
			//Top
			pGray.setTextAlign(Paint.Align.valueOf("CENTER"));
			canvas.drawText(texts[1], 1 + getWidth() / 2, 20, pGray);
			break;
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
	{
		setMeasuredDimension(77, 73);
	}
}

class SpaceButton extends KeyButton {
	public SpaceButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
	{
		setMeasuredDimension(154, 73);
	}
}